package com.hm.cms.auto;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.TaskExecutor;
import org.springframework.remoting.httpinvoker.HttpInvokerProxyFactoryBean;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import com.hm.cms.service.AdvertService;
import com.hm.cms.service.ArticleService;
import com.hm.cms.service.BlacklistService;
import com.hm.cms.service.BoosSerice;
import com.hm.cms.service.ExtLinkService;
import com.hm.cms.service.TermTaxonomyService;
import com.hm.cms.service.TermTayRelationshipsService;
import com.hm.cms.service.TermsService;
import com.hm.cms.service.VisitIpStatisticsService;

/**
 * @author shishun.wang
 * @date 上午11:43:29 2017年9月27日
 * @version 1.0
 * @describe
 */
@EnableAsync // 配置 @Async 异步生效
@Configuration
@EnableConfigurationProperties({ SystemEnvConfigProperties.class })
public class HmServerConfiguration {

	@Autowired
	private SystemEnvConfigProperties systemEnvConfigProperties;

	@Bean
	public TaskExecutor taskExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(10);
		executor.setKeepAliveSeconds(300);
		executor.setMaxPoolSize(150);
		executor.setQueueCapacity(25);
		return executor;
	}

	/**** rpc invoker call *******/

	@Bean
	public AdvertService advertService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getHmcmsHost()
				+ AdvertService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(AdvertService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (AdvertService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public ArticleService articleService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getHmcmsHost()
				+ ArticleService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(ArticleService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (ArticleService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public BoosSerice boosSerice() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getHmcmsHost()
				+ BoosSerice.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(BoosSerice.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (BoosSerice) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public ExtLinkService extLinkService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getHmcmsHost()
				+ ExtLinkService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(ExtLinkService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (ExtLinkService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public TermsService termsService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getHmcmsHost()
				+ TermsService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(TermsService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (TermsService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public TermTaxonomyService termTaxonomyService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getHmcmsHost()
				+ TermTaxonomyService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(TermTaxonomyService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (TermTaxonomyService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public TermTayRelationshipsService termTayRelationshipsService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getHmcmsHost()
				+ TermTayRelationshipsService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(TermTayRelationshipsService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (TermTayRelationshipsService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public VisitIpStatisticsService visitIpStatisticsService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getHmcmsHost()
				+ VisitIpStatisticsService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(VisitIpStatisticsService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (VisitIpStatisticsService) httpInvokerProxyFactoryBean.getObject();
	}

	@Bean
	public BlacklistService blacklistService() {
		HttpInvokerProxyFactoryBean httpInvokerProxyFactoryBean = new HttpInvokerProxyFactoryBean();
		httpInvokerProxyFactoryBean.setServiceUrl(systemEnvConfigProperties.getHttpInvokerProvider().getHmcmsHost()
				+ BlacklistService.HTTP_INVOKER_EXPORT_CONTEXT);
		httpInvokerProxyFactoryBean.setServiceInterface(BlacklistService.class);
		httpInvokerProxyFactoryBean.afterPropertiesSet();
		return (BlacklistService) httpInvokerProxyFactoryBean.getObject();
	}
}
